package controller

import (
	backend "AccessAdmin/api/backend"
	"AccessAdmin/internal/model"
	"AccessAdmin/internal/model/entity"
	"AccessAdmin/internal/service"
	"context"
	"github.com/gogf/gf/v2/encoding/gjson"
	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/util/gconv"
)

// AttachmentCate Attachment 附件资源
var AttachmentCate = cAttachmentCate{}

type cAttachmentCate struct{}

// ShowIndex 渲染附件资源分类列表
func (c *cAttachmentCate) ShowIndex(ctx context.Context, req *backend.AttachmentCateIndexReq) (res *backend.AttachmentCateIndexRes, err error) {
	service.View().Render(ctx, model.View{
		Title: "资源分类列表",
	})
	return
}

// IndexData 获取附件资源分类列表
func (c *cAttachmentCate) IndexData(ctx context.Context, req *backend.AttachmentCateListReq) (res *backend.AttachmentCateListRes, err error) {
	res = &backend.AttachmentCateListRes{}
	_data, err := service.AttachmentCate().GetList(ctx, model.AttachmentCateGetListInput{
		Page:   req.Page,
		Size:   req.Limit,
		Title:  req.Title,
		Status: req.Status,
	})
	if _data != nil && err == nil {
		res.Data = _data.List
		res.Count = _data.Total
	}
	return
}

// ShowCreate 显示附件资源创建
func (c *cAttachmentCate) ShowCreate(ctx context.Context, req *backend.AttachmentCateShowCreateReq) (res *backend.AttachmentCateShowCreateRes, err error) {
	cateTree, err := service.AttachmentCate().GetCateTree(ctx)
	if err != nil {
		return
	}
	jsonData, _ := gjson.EncodeString(cateTree)
	service.View().Render(ctx, model.View{
		Title:   "资源分类创建",
		MainTpl: "admin/attachment-cate/form.html",
		Data: g.Map{
			"action": "/admin/attachment-cate/create",
			"attachmentCateModel": &entity.AttachmentCate{
				Sort:   100,
				Status: 1,
			},
			"cateTree": jsonData,
		},
	})
	return
}

// Create 创建分类
func (c *cAttachmentCate) Create(ctx context.Context, req *backend.AttachmentCateCreateReq) (res *backend.AttachmentCateCreateRes, err error) {
	out, err := service.AttachmentCate().Create(ctx, model.AttachmentCateCreateInput{
		AttachmentCateCreateUpdateBase: model.AttachmentCateCreateUpdateBase{
			Pid:    req.Pid,
			Title:  req.Title,
			Status: req.Status,
			Sort:   req.Sort,
		},
	})
	if err != nil {
		return nil, err
	}
	return &backend.AttachmentCateCreateRes{AttachmentCateId: out.CateId}, nil
}

// ShowUpdate 显示分类修改
func (c *cAttachmentCate) ShowUpdate(ctx context.Context, req *backend.AttachmentCateShowUpdateReq) (res *backend.AttachmentCateShowUpdateRes, err error) {
	cateModel, err := service.AttachmentCate().GetDetail(ctx, model.AttachmentCateGetDetailInput{
		Id: req.Id,
	})
	if err != nil {
		return nil, gerror.New("分类信息错误!")
	}
	cateTree, err := service.AttachmentCate().GetCateTree(ctx)
	if err != nil {
		return
	}
	jsonData, _ := gjson.EncodeString(cateTree)
	service.View().Render(ctx, model.View{
		Title:   "资源分类修改",
		MainTpl: "admin/attachment-cate/form.html",
		Data: g.Map{
			"action":              "/admin/attachment-cate/update/" + gconv.String(cateModel.Id),
			"attachmentCateModel": cateModel,
			"cateTree":            jsonData,
		},
	})
	return
}

// Update 修改分类
func (c *cAttachmentCate) Update(ctx context.Context, req *backend.AttachmentCateUpdateReq) (res *backend.AttachmentCateUpdateRes, err error) {
	err = service.AttachmentCate().Update(ctx, model.AttachmentCateUpdateInput{
		Id: req.Id,
		AttachmentCateCreateUpdateBase: model.AttachmentCateCreateUpdateBase{
			Pid:    req.Pid,
			Title:  req.Title,
			Status: req.Status,
			Sort:   req.Sort,
		},
	})
	return
}

// Delete 删除分类
func (c *cAttachmentCate) Delete(ctx context.Context, req *backend.AttachmentCateDeleteReq) (res *backend.AttachmentCateDeleteRes, err error) {
	err = service.AttachmentCate().Delete(ctx, model.AttachmentCateGetDetailInput{
		Id: req.Id,
	})
	return
}
